﻿using System;
using System.Collections.Generic;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;

namespace SGDemo.Permission
{
    public partial class ShortcutsList : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);
            if(!this.IsPostBack)
            {
                BindComBoBox();
            }
        }
        /// <summary>
        /// 添加快捷方式
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [AjaxPro.AjaxMethod]
        public bool SaveFastAdd()
        {
            string userkeyid = ApplicationContext.Current.UserKeyid;
            string pagecode = this.GetType().Name;
            PageEntity page = _permissionService.Page_GetKeyidBypagename(pagecode);
            if (page != null)
            {
                return _permissionService.Shortcuts_Add(userkeyid, page.Keyid);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 模块下拉框数据绑定
        /// </summary>
        public void BindComBoBox()
        {
            List<ModuleEntity> moduleList = _permissionService.Module_GetAll();
            foreach (ModuleEntity list in moduleList)
            {
                ListEditItem listbox = new ListEditItem();
                listbox.Text = list.ModuleName;
                listbox.Value = list.Keyid;
                cbModule.Items.Add(listbox);
            }

        }

        /// <summary>
        /// 为当前用户添加一项快捷方式
        /// </summary>
        /// <param name="pageKeyid">页面Keyid</param>
        /// <param name="isSelected">是否选中</param>
        /// <returns>是否操作成功</returns>
        [AjaxPro.AjaxMethod]
        public bool CheckedChangedPage(string pageKeyid, bool isSelected)
        {
            if (isSelected)
            {
                return _permissionService.Shortcuts_Add(ApplicationContext.Current.UserKeyid, pageKeyid);
            }
            else
            {
                return _permissionService.Shortcuts_Delete(ApplicationContext.Current.UserKeyid, pageKeyid);
            }
        }

        /// <summary>
        /// 自定义GridView回调重新绑定
        /// </summary>
        protected void gvwList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            gvwList.PageIndex = 0;
            gvwList.DataBind();
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        protected void btnExporter_Click(object sender, EventArgs e)
        {
            ASPxButton btn = sender as ASPxButton;
            string fileType = btn.CommandName;
            switch (fileType)
            {
                case "Xls": this.gvweList.WriteXlsToResponse(); break;
                case "Xlsx": this.gvweList.WriteXlsxToResponse(); break;
                case "Pdf": this.gvweList.WritePdfToResponse(); break;
                case "Csv": this.gvweList.WriteCsvToResponse(); break;
                case "Rtf": this.gvweList.WriteRtfToResponse(); break;
                default: break;
            }
        }

        /// <summary>
        /// 删除快捷方式
        /// </summary>
        [AjaxPro.AjaxMethod]
        public bool BulkDelete(string keyids)
        {
            return _permissionService.Shortcuts_Delete(ApplicationContext.Current.UserKeyid, keyids);
        }
    }

    /// <summary>
    /// 数据源
    /// </summary>
    public class ShortcutsListDataSource
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<PermissionDetails> GetAllByUserKeyid(string filterConditions)
        {
            return _permissionService.Shortcuts_GetAllByUserKeyid(ApplicationContext.Current.UserKeyid, filterConditions);
        }

        public List<PermissionDetails> GetListByUserKeyid(string filterConditions)
        {
            return _permissionService.Shortcuts_GetListByUserKeyid(ApplicationContext.Current.UserKeyid, filterConditions);
        }
    }

        
}